﻿using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace DrawTools.Forms
{
  public class RoundedForm : Form
  {
    #region Win API

    [DllImport("Gdi32.dll", EntryPoint = "CreateRoundRectRgn")]
    private static extern IntPtr CreateRoundRectRgn(int nLeftRect, // x-coordinate of upper-left corner
                                                    int nTopRect, // y-coordinate of upper-left corner
                                                    int nRightRect, // x-coordinate of lower-right corner
                                                    int nBottomRect, // y-coordinate of lower-right corner
                                                    int nWidthEllipse, // height of ellipse
                                                    int nHeightEllipse // width of ellipse
      );

    #endregion

    private bool _drag;
    private Point _startPoint = new Point(0, 0);

    protected override CreateParams CreateParams
    {
      get
      {
        const int CS_DROPSHADOW = 0x20000;
        CreateParams cp = base.CreateParams;
        cp.ClassStyle |= CS_DROPSHADOW;
        return cp;
      }
    }

    //public RoundedForm()
    //{
    //  Padding = new Padding(20);
    //  FormBorderStyle = FormBorderStyle.None;
    //  Region = Region.FromHrgn(CreateRoundRectRgn(0, 0, Width - 8, Height - 8, 16, 16)); // adjust these parameters to get the look you want.
    //}

    protected override void OnLoad(EventArgs e)
    {
      base.OnLoad(e);

      Padding = new Padding(20);
      FormBorderStyle = FormBorderStyle.None;
      Region = Region.FromHrgn(CreateRoundRectRgn(0, 0, Width - 8, Height - 8, 16, 16)); // adjust these parameters to get the look you want.
    }

    protected override void OnPaintBackground(PaintEventArgs e)
    {
      base.OnPaintBackground(e);

      //var brush = new LinearGradientBrush(new Point(0, 0), new Point(0, 30), Color.FromArgb(52, 58, 63), Color.FromArgb(41, 46, 49));
      //e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(41, 46, 49)), new Rectangle(new Point(0, 0), new Size(this.Width, this.Width)));
      //e.Graphics.FillRectangle(brush, new Rectangle(new Point(0, 0), new Size(this.Width, 30)));

      var brush = new LinearGradientBrush(new Point(0, 0), new Point(0, 30), SystemColors.Control, SystemColors.ControlLight);
      e.Graphics.FillRectangle(new SolidBrush(SystemColors.ControlLight), new Rectangle(new Point(0, 0), new Size(this.Width, this.Width)));
      e.Graphics.FillRectangle(brush, new Rectangle(new Point(0, 0), new Size(this.Width, 30)));
    }

    protected override void OnResize(EventArgs e)
    {
      base.OnResize(e);
      Region = Region.FromHrgn(CreateRoundRectRgn(0, 0, Width - 8, Height - 8, 16, 16)); // adjust these parameters to get the look you want.
    }

    #region Drag Capture Window

    protected override void OnMouseDown(MouseEventArgs e)
    {
      base.OnMouseDown(e);

      _drag = true;
      _startPoint = new Point(e.X, e.Y);
    }

    protected override void OnMouseMove(MouseEventArgs e)
    {
      base.OnMouseMove(e);

      if (!_drag)
        return;

      Point screenMousePosition = PointToScreen(e.Location);
      Location = new Point(screenMousePosition.X - _startPoint.X, screenMousePosition.Y - _startPoint.Y);
    }

    protected override void OnMouseUp(MouseEventArgs e)
    {
      base.OnMouseUp(e);

      _drag = false;
    }

    #endregion
  }
}